package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.DWaybillStatusHistoryService;
import cn.huiyunche.base.service.mappers.DWaybillStatusHistoryMapper;
import cn.huiyunche.base.service.model.DWaybillStatusHistory;
import cn.huiyunche.base.service.model.DWaybillStatusHistoryExample;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @FileName: cn.huiyunche.service.base.interfaces.tms.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/11/3 上午10:14
 */
@Service
public class DWaybillStatusHistoryServiceImpl implements DWaybillStatusHistoryService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DWaybillStatusHistoryServiceImpl.class);

    @Autowired
    private DWaybillStatusHistoryMapper dWaybillStatusHistoryMapper;

    @Override
    public void add(Long waybillId, String statusType, String currentStatus, String lastStatus, Long userId, String userName) {
        LOGGER.info("add params : {}, {}, {}, {}, {}", waybillId, currentStatus, lastStatus, userId, userName);
        if (null == waybillId) {
            LOGGER.error("add param waybillId must not be null");
            throw new IllegalArgumentException("运单主键不能为空");
        }
        if (StringUtils.isBlank(currentStatus)) {
            LOGGER.error("add param currentStatus must not be null");
            throw new IllegalArgumentException("运单当前状态不能为空");
        }
        if (null == userId) {
            LOGGER.error("add param userId must not be null");
            throw new IllegalArgumentException("用户主键不能为空");
        }
        if (StringUtils.isBlank(userName)) {
            LOGGER.error("add param userName must not be null");
            throw new IllegalArgumentException("用户名称不能为空");
        }

        DWaybillStatusHistory history = new DWaybillStatusHistory();
        history.setWaybillId(waybillId);
        history.setStatusType(statusType);
        history.setCurrentStatus(currentStatus);
        if (StringUtils.isNotBlank(lastStatus)) {
            history.setLastStatus(lastStatus);
        }
        history.setUserId(userId);
        history.setUserName(userName);

        dWaybillStatusHistoryMapper.insertSelective(history);
    }

    @Override
    public DWaybillStatusHistory getWaybillStatusHistoryById(Long waybillId, String statusType, String currentStatus) {
        DWaybillStatusHistory dwaybillStatusHistory = null;

        DWaybillStatusHistoryExample example = new DWaybillStatusHistoryExample();
        example.createCriteria().andWaybillIdEqualTo(waybillId).andStatusTypeEqualTo(statusType).
                andCurrentStatusEqualTo(currentStatus);
        List<DWaybillStatusHistory> dwaybillStatusHistorys = this.dWaybillStatusHistoryMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(dwaybillStatusHistorys)) {
            dwaybillStatusHistory = dwaybillStatusHistorys.get(0);
        }
        return dwaybillStatusHistory;
    }
}
